package utn.frsf.gabinete.fw;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.servlet.http.*;

import org.codehaus.jackson.map.ObjectMapper;

import utn.frsf.gabinete.fw.dao.GenericDAO;
import utn.frsf.gabinete.fw.generator.PageGenerator;
import utn.frsf.gabinete.fw.sample.Auto;
import utn.frsf.gabinete.fw.sample.Entidad2;

/**
 * Agregar 
 * http://code.google.com/p/google-guice/wiki/ServletModule
 * 
 * para cargar el modulo de grabado automatico
 * @author Administrador
 *
 */
@SuppressWarnings("serial")
public class Html5fwControllerServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp){}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		String nombreClase = "utn.frsf.gabinete.fw.sample."+req.getParameter("ent");
		GenericDAO dao = new GenericDAO();
		ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
		Object obj = null;
		try {
			obj = dao.crearInstancia(mapper.readValue(req.getParameter("datos"),Class.forName(nombreClase)));
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		System.out.println("obj"+obj.toString()+" / "+mapper.writeValueAsString(obj));
		resp.setContentType("application/json");
		mapper.writeValue(resp.getWriter(), mapper.writeValueAsString(obj));
	}
}
